package com.dgut.shopManage.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.common.util.Audit;
import com.dgut.common.util.Rent;
import com.dgut.shopManage.dao.ApplyRepository;
import com.dgut.shopManage.dao.MerchantRepository;
import com.dgut.shopManage.dao.ResultRepository;
import com.dgut.shopManage.dao.ShopRepository;
import com.dgut.shopManage.entity.Apply;
import com.dgut.shopManage.entity.Merchant;
import com.dgut.shopManage.entity.Results;
import com.dgut.shopManage.entity.Shop;
import com.dgut.shopManage.entity.dto.ApplyDTO;

@Service
@Transactional
public class ApplyService implements IApplyService {
	@Autowired
	private ApplyRepository applyRepository;
	@Autowired
	private ShopRepository shopRepository;
	@Autowired
	private MerchantRepository merchantRepository;
	@Autowired
	private ResultRepository resultsRepository;
	public void saveOrUpdate(ApplyDTO dto) 
	{
		Apply apply = new Apply();
		ApplyDTO.dtoToEntity(dto, apply);
		Shop shop = shopRepository.findOne(dto.getShopId());
		apply.setShop(shop);
		shop.getApplys().add(apply);
		applyRepository.save(apply);
	}
	public void delete(Apply apply) {
		apply.setShop(null);
		applyRepository.delete(apply);
	}
	public  Apply findOne(Long id) {
		return applyRepository.findOne(id);
	}
	public List<ApplyDTO> findAll(){
		List<Apply> list = (List<Apply>) applyRepository.findAll();
		List<ApplyDTO> dtoLists = new ArrayList<ApplyDTO>();
		for (Apply entity:list) {
			ApplyDTO dto = new ApplyDTO();
			ApplyDTO.entityToDto(entity, dto);
			dtoLists.add(dto);
		}
		return dtoLists;
	}
	public Page<ApplyDTO> findAll(Specification<Apply> spec, Pageable pageable) {
		Page<Apply> page = applyRepository.findAll(spec,pageable);

		List<ApplyDTO> dtoLists = new ArrayList<ApplyDTO>();
		for(Apply entity : page.getContent()) {
			ApplyDTO dto = new ApplyDTO();
			ApplyDTO.entityToDto(entity, dto);
			dtoLists.add(dto);
		}
		return new PageImpl<ApplyDTO>(dtoLists,pageable,page.getTotalElements());
	}
	public Page<ApplyDTO>findByShop(Long shopId,Pageable pageable){
		Page<Apply> list =  applyRepository.findByShop(shopId, pageable);
		List<ApplyDTO> dtoLists = new ArrayList<ApplyDTO>();
		for (Apply entity : list.getContent()) {
			ApplyDTO dto = new ApplyDTO();
			ApplyDTO.entityToDto(entity, dto);
			dtoLists.add(dto);
		}
		return new PageImpl<ApplyDTO>(dtoLists, pageable, list.getTotalElements());
	}
	public Page<ApplyDTO> findAll(Pageable pageable) {
		
		Page<Apply> list = applyRepository.findAll(pageable);
		List<ApplyDTO> dtoLists = new ArrayList<ApplyDTO>();
		for (Apply entity : list.getContent()) {
			ApplyDTO dto = new ApplyDTO();
			ApplyDTO.entityToDto(entity, dto);
			dtoLists.add(dto);
		}
		return new PageImpl<ApplyDTO>(dtoLists, pageable, list.getTotalElements());
	}
	public void audit(Long id) {
		Apply apply = applyRepository.findOne(id);
		Shop shop = shopRepository.findOne(apply.getShop().getId());
		Merchant merchant = new Merchant();
		merchant.setMerchantName(apply.getmName());
		merchant.setTelephone(apply.getTphone());
		merchant.setsRentTime(apply.getsTime());
		merchant.seteRentTime(apply.geteTime());
		merchant.setXtype(apply.getType());
		merchant.setShopName(apply.getsName());
		shop.setMerchant(merchant);
		shop.setRent(Rent.已租用);
		merchantRepository.save(merchant);
	}
	public void pass(Long id) {
		Results results = new Results();
		Apply apply = applyRepository.findOne(id);
		Shop shop = shopRepository.findOne(apply.getShop().getId());
		results.setsNo(shop.getShopNo());
		results.setSsName(apply.getsName());
		results.setTtphone(apply.getTphone());
		results.setAaTime(apply.getaTime());
		results.setMmName(apply.getmName());
		results.setTtype(apply.getType());
		results.setAuditRent(Audit.审核通过);
		resultsRepository.save(results);
	}
	public void passNo(Long id) {
		Results results = new Results();
		Apply apply = applyRepository.findOne(id);
		Shop shop = shopRepository.findOne(apply.getShop().getId());
		results.setsNo(shop.getShopNo());
		results.setSsName(apply.getsName());
		results.setTtphone(apply.getTphone());
		results.setAaTime(apply.getaTime());
		results.setMmName(apply.getmName());
		results.setTtype(apply.getType());
		results.setAuditRent(Audit.审核不通过);
		resultsRepository.save(results);
	}
}
